package com.linkin.homepage.base.loading;


import android.graphics.Color;
import android.view.View;

import com.linkin.homepage.R;
import com.linkin.homepage.base.util.ClickUtils;
import com.linkin.homepage.base.util.NetStatusUtils;
import com.linkin.homepage.util.ToastUitl;

public class LoadingView {

    private View mNormalView;
    private View mLoadingView;
    private View mFailedView;
    private View mNoDataView;
    private View mParentContainer;
    private ILoadingAnimation mLoadingAnimation;

    public LoadingView(View view, View.OnClickListener failedListener) {
        mParentContainer = view.findViewById(R.id.loading_container);
        mNormalView = view.findViewById(R.id.normal_view);
        mLoadingView = view.findViewById(R.id.loading_view);
        mFailedView = view.findViewById(R.id.failed_view);
        mNoDataView = view.findViewById(R.id.no_data_view);

        if (mLoadingView != null) {
            mLoadingView.setOnClickListener(v -> {});
        }
        if (mFailedView != null) {
            mFailedView.setOnClickListener(v -> {
            });
            if (failedListener != null) {
                View v = mFailedView.findViewById(R.id.failed_click_view);
                if (v != null) {
                    v.setOnClickListener(v1 -> {
                        if (ClickUtils.check(v1)) {
                            if (NetStatusUtils.isConnected(v1.getContext())) {
                                failedListener.onClick(v1);
                            } else {
                                ToastUitl.showShort("网络未连接，请检查网络设置后重试！");
                            }
                        }
                    });
                }
            }
        }
        if (mNoDataView != null) {
            mNoDataView.setOnClickListener(v -> {
            });
        }
    }

    public void setLoadingAnimation(ILoadingAnimation loadingAnimation) {
        mLoadingAnimation = loadingAnimation;
    }

    public void setBackgroundColor(int color) {
        if (mLoadingView != null) {
            if (color == Color.TRANSPARENT) {
                mLoadingView.setBackground(null);
            } else {
                mLoadingView.setBackgroundColor(color);
            }
        }
        if (mFailedView != null) {
            if (color == Color.TRANSPARENT) {
                mFailedView.setBackground(null);
            } else {
                mFailedView.setBackgroundColor(color);
            }
        }
        if (mNoDataView != null) {
            if (color == Color.TRANSPARENT) {
                mNoDataView.setBackground(null);
            } else {
                mNoDataView.setBackgroundColor(color);
            }
        }
    }

    public void setNoDataTitle(String noDataStr, View.OnClickListener listener) {
    }

    public boolean isShowingNormal() {
        return !isShowingLoading() && !isShowingFailed() && !isShowingNoData();
    }

    public boolean isShowingLoading() {
        return null != mLoadingView && mLoadingView.getVisibility() == View.VISIBLE;
    }

    public boolean isShowingFailed() {
        return mFailedView != null && mFailedView.getVisibility() == View.VISIBLE;
    }

    public boolean isShowingNoData() {
        return mNoDataView != null && mNoDataView.getVisibility() == View.VISIBLE;
    }

    public void showLoading(int type) {
        if (mParentContainer != null) {
            mParentContainer.setVisibility(type == 0 ? View.GONE : View.VISIBLE);
        }
        if (mNormalView != null) {
            mNormalView.setVisibility(type == 0 ? View.VISIBLE : View.GONE);
        }
        if (mLoadingView != null) {
            if (mLoadingAnimation != null) {
                if (type == 1) {
                    mLoadingAnimation.startLoading();
                    mLoadingView.setVisibility(View.VISIBLE);
                } else {
                    mLoadingView.setVisibility(View.GONE);
                    mLoadingAnimation.stopLoading();
                }
            } else {
                mLoadingView.setVisibility(type == 1 ? View.VISIBLE : View.GONE);
            }
        }
        if (mFailedView != null) {
            mFailedView.setVisibility(type == 2 ? View.VISIBLE : View.GONE);
        }
        if (mNoDataView != null) {
            mNoDataView.setVisibility(type == 3 ? View.VISIBLE : View.GONE);
        }
    }
}
